package com.lemontree.common.utils.redis;

import com.lemontree.common.utils.SpringContextUtil;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

/**
 * @Author: YLBG-YCY-1325
 * @Description:
 * @Date: 2017/3/27
 */
public class RedisManager {
    private static Logger LOG = Logger.getLogger(RedisManager.class);
    // 配置池
    private static JedisPool jedisPool;
    // 获取小标（根据键来获取值时可能或获取到多个值，此时取第一个）
    private static final int INDEX = 0;

    // 初始化池
    public static void initPool() throws Exception {
        if (jedisPool == null) {
            Object pool = SpringContextUtil.getBean("jedisPool",JedisPool.class);
            if (pool == null) {
                throw new Exception("init pool error, bean is null");
            }
            jedisPool = (JedisPool) pool;
        }
    }

    /**
     * 获取jedis
     * @return
     */
    public static Jedis getJedis() {
        Jedis jedis = null;
        try {
            initPool();
            jedis = getJedisPool().getResource();
        } catch (Exception e) {
            throw new JedisConnectionException(e);
        }
        return jedis;
    }

    /**
     * 根据键获取字符值
     * @param key 键
     * @return 字符值
     * @throws Exception
     */
    public static String getString(String key) throws Exception {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = getJedis();
            jedis.select(INDEX);
            result = jedis.get(key);
        } catch (Exception e) {
            throw e;
        } finally {
            closeRedis(jedis);
        }
        return result;
    }

    /**
     * 设置key-value
     * @param key 键
     * @param value 值
     * @throws Exception
     */
    public static void setString(String key, String value) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.select(INDEX);
            jedis.set(key, value);
        } catch (Exception e) {
            throw e;
        } finally {
            closeRedis(jedis);
        }
    }

    public static void delByKey(String key) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.select(INDEX);
            jedis.del(key);
        } catch (Exception e) {
            throw e;
        } finally {
            closeRedis(jedis);
        }
    }

    /**
     * 设置key-value以及有效时间
     * @param key 键
     * @param value 值
     * @param expireSeconds 存在时间 单位：秒
     * @throws Exception
     */
    public static void setStringExpire(String key, String value, int expireSeconds)
            throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.select(INDEX);
            jedis.set(key, value);
            jedis.expire(key, expireSeconds);
        } catch (Exception e) {
            throw e;
        } finally {
            closeRedis(jedis);
        }
    }

    /**
     * 关闭redis资源
     * @param jedis
     */
    public static void closeRedis(Jedis jedis) {
        if (jedis == null) {
            return;
        }
        jedis.close();
    }

    public static JedisPool getJedisPool() {
        return jedisPool;
    }

    public static void setJedisPool(JedisPool jedisPool) {
        RedisManager.jedisPool = jedisPool;
    }
}
